package com.situ.allaia.controller;

import com.situ.allaia.model.Good;
import com.situ.allaia.service.GoodService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;


@Controller
@RequestMapping("/good")
public class GoodController {
    private final GoodService goodService;

    public GoodController(GoodService goodService) {
        this.goodService = goodService;
    }

    @GetMapping("/list")
    public String list() {
        return "good/list";
    }

    @GetMapping("/add")
    public String add() {
        return "good/add";
    }

    @GetMapping("/edit")
    public String edit(Integer id, Map<String, Object> map) {
        if (id == null) {
            map.put("error", "未指定要修改的商品编号");
            return "good/edit";
        }

        Good good = goodService.findById(id);
        if (good == null) {
            map.put("error", "未指定要修改的商品编号");
            return "good/edit";
        }

        map.put("good", good);

        return "good/edit";
    }
}
